package com.jixiantubu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gemptc.bean.PostInfoBean;
import com.gemptc.bean.ReplyBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.entity.Post_InfoDaoimp;

@WebServlet("/post_servlet")
public class Post_servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public Post_servlet() {
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		String value = request.getParameter("key");
		Post_InfoDaoimp post = new Post_InfoDaoimp();
		Gson gson = new Gson();
		Type typeOfSrc = new TypeToken<List<ReplyBean>>() {
		}.getType();
		String scan;
		String topicid;
		System.out.println(value);
		switch (value) {
		case "1":
			List<PostInfoBean> list = new ArrayList<PostInfoBean>();
			String pageNow = request.getParameter("page");
			list = post.select(Integer.parseInt(pageNow));
			String result = gson.toJson(list, typeOfSrc);
			printWriter.write(result);
			break;
		case "2":
			scan=request.getParameter("scan");
			topicid=request.getParameter("topicid");
			post.insertScan(Integer.parseInt(scan),Integer.parseInt(topicid));
			break;
		case "3":
			String page = request.getParameter("page");
			List<PostInfoBean> list4 = post
					.selectIsfine(Integer.parseInt(page));
			String isfinePost = gson.toJson(list4, typeOfSrc);
			printWriter.write(isfinePost);
			break;
		default:
			break;
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
